package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.ImageUploadUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {
    @Reference
    private ExtCproductService extCproductService;

    @Reference
    private FactoryService factoryService;

    /*
        分页查询所有的附件信息
            地址：/cargo/extCproduct/list.do
            参数：contractId(合同ID) contractProductId（货物ID）
            返回值：跳转到分页页面
     */
    @RequestMapping("/list")
    public String findAll(
            @RequestParam(defaultValue = "1")int page,
            @RequestParam(defaultValue = "5")int size,
            String contractId,
            String contractProductId){
        //查询所有的附件生产厂家信息
        FactoryExample example = new FactoryExample();
        FactoryExample.Criteria criteria = example.createCriteria();
        criteria.andCtypeEqualTo("附件");
        List<Factory> list = factoryService.findAll(example);
        //将生产厂家信息保存到request域中带到页面展示
        request.setAttribute("factoryList",list);

        //设置查询附件信息的条件
        ExtCproductExample ece = new ExtCproductExample();
        ExtCproductExample.Criteria eceCriteria = ece.createCriteria();
        eceCriteria.andContractProductIdEqualTo(contractProductId);
        //根据货物ID查询所有的附件信息
        PageInfo info = extCproductService.findAll(ece,page,size);
        //将数据保存到request域中
        request.setAttribute("page",info);
        //为了方便查询 将合同ID与货物ID保存到request域中
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);
        return "cargo/extc/extc-list";
    }

    /*
        新增或修改附件信息
     */
    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto) throws IOException {
       //赋值当前登录企业的企业ID与企业名称
        extCproduct.setCompanyId(getLoginCompanyId());
        extCproduct.setCompanyName(getLoginCompanyName());
        //判断当前附件是否用ID
        if (StringUtils.isEmpty(extCproduct.getId())){
            //id为空  新增附件
            //判断是否有上传图片信息
            if (!productPhoto.isEmpty()){
                //执行图片上传
                String url = new ImageUploadUtils().upload(productPhoto.getBytes());
                //保存图片地址
                extCproduct.setProductImage(url);
            }
            //保存附件信息
            extCproductService.save(extCproduct);
        }else {
            //执行修改附件信息
            extCproductService.update(extCproduct);
        }
        return  "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }

    /*
        根据附件ID查询附件信息
        地址：/cargo/extCproduct/toUpdate.do
            参数：附件ID 合同ID 货物ID
            返回值：跳转到修改附件页面
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id,String contractId,String contractProductId){
        //查询附件厂家展示
        FactoryExample example = new FactoryExample();
        FactoryExample.Criteria criteria = example.createCriteria();
        criteria.andCtypeEqualTo("附件");
        //查询附件生产厂家
        List<Factory> list = factoryService.findAll(example);
        //保存到request域中
        request.setAttribute("factoryList",list);

        //根据ID查询附件信息
        ExtCproduct extCproduct = extCproductService.findById(id);
        //将附件信息保存到request域中带 到页面展示
        request.setAttribute("extCproduct",extCproduct);
        //将合同ID与货物ID保存到request域中
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);
        //跳转到修改页面展示
        return "cargo/extc/extc-update";
    }

    /*
        根据附件ID删除
     */
    @RequestMapping("/delete")
    public String delete(String id,String contractId,String contractProductId){
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }
}
